import os
from django.test import TestCase
from django.conf import settings

from ipproxy.models import IpProxyModel
from ipproxy.tasks import resolving_html, check_url

# Create your tests here.
ip_list = [
    {"ip": "60.168.207.21",
    "port": "1133",
    "niming": "高匿名",
    "method": "HTTP",
    "addr": "安徽省合肥市  电信",
    "speed": "1秒",
    "last_time": "2020-07-24 08:31:01",
    },{
    "ip": "113.194.135.84",
    "port": "9999",
    "niming": "高匿名",
    "method": "HTTP",
    "addr": "江西省宜春市  联通",
    "speed": "0.5秒",
    "last_time": "2020-07-24 07:31:01",
    },{
    "ip": "110.243.10.249",
    "port": "9999",
    "niming": "高匿名",
    "method": "HTTP",
    "addr": "河北省唐山市  联通",
    "speed": "2秒",
    "last_time": "2020-07-24 06:31:01",
    },{
    "ip": "221.2.155.35",
    "port": "8060",
    "niming": "高匿名",
    "method": "HTTP",
    "addr": "中国 山东 威海 联通",
    "speed": "1秒",
    "last_time": "2020-07-24 05:31:01",
    },{
    "ip": "61.183.176.122",
    "port": "57210",
    "niming": "高匿名",
    "method": "HTTP",
    "addr": "湖北省武汉市  电信",
    "speed": "3秒",
    "last_time": "2020-07-24 04:31:01",
    },{
    "ip": "113.120.35.129",
    "port": "9999",
    "niming": "高匿名",
    "method": "HTTP",
    "addr": "山东省济南市  电信",
    "speed": "3秒",
    "last_time": "2020-07-24 03:31:02",
    },{
    "ip": "125.108.100.79",
    "port": "9000",
    "niming": "高匿名",
    "method": "HTTP",
    "addr": "浙江省温州市  电信",
    "speed": "3秒",
    "last_time": "2020-07-24 02:31:02",
    },{
    "ip": "113.194.28.102",
    "port": "9999",
    "niming": "高匿名",
    "method": "HTTP",
    "addr": "江西省新余市  联通",
    "speed": "0.4秒",
    "last_time": "2020-07-24 01:31:01",
    },{
    "ip": "114.239.2.178",
    "port": "9999",
    "niming": "高匿名",
    "method": "HTTP",
    "addr": "江苏省宿迁市  电信",
    "speed": "2秒",
    "last_time": "2020-07-24 00:31:01",
    },{
    "ip": "113.194.20.35",
    "port": "9999",
    "niming": "高匿名",
    "method": "HTTP",
    "addr": "江西省新余市  联通",
    "speed": "1秒",
    "last_time": "2020-07-23 23:31:01",
    },{
    "ip": "124.117.103.14",
    "port": "8118",
    "niming": "高匿名",
    "method": "HTTP",
    "addr": "新疆维吾尔自治区乌鲁木齐市  电信",
    "speed": "3秒",
    "last_time": "2020-07-23 22:31:02",
    },{
    "ip": "171.35.175.42",
    "port": "9999",
    "niming": "高匿名",
    "method": "HTTP",
    "addr": "江西省宜春市  联通",
    "speed": "1秒",
    "last_time": "2020-07-23 21:31:01",
    },{
    "ip": "113.195.233.154",
    "port": "9999",
    "niming": "高匿名",
    "method": "HTTP",
    "addr": "江西省新余市  联通",
    "speed": "2秒",
    "last_time": "2020-07-23 20:31:01",
    },{
    "ip": "171.35.172.51",
    "port": "9999",
    "niming": "高匿名",
    "method": "HTTP",
    "addr": "江西省宜春市  联通",
    "speed": "0.7秒",
    "last_time": "2020-07-23 19:31:01",
    }
]

def clean_redis_test_data(datas):
    # 清理测试数据
    for data in datas:
        index = IpProxyModel().search(data) 
        if index >=0:
            num = IpProxyModel().remove(IpProxyModel().index(index))


class ModelTest(TestCase):
    def test_ipproxymodel(self):
        # 清理测试数据
        clean_redis_test_data(ip_list)
        old_count = IpProxyModel().length()
        # 测试插入list 输入数据
        self.assertEqual(IpProxyModel().lpush(*ip_list), len(ip_list) + old_count, '插入list 失败')
        # 测试删除数据
        test_data = ip_list[0]
        self.assertEqual(IpProxyModel().remove(test_data), 1, '删除存在数据失败')
        self.assertEqual(IpProxyModel().remove(test_data), 0, '删除不存在的数据失败')
        # test method index
        self.assertIsInstance(IpProxyModel().index(1), dict, 'TypeError, index 需要响应一个dict ')
        # 测试search 
        index = IpProxyModel().search(ip_list[5])
        self.assertEqual(ip_list[5], IpProxyModel().index(index), '查询存在的数据失败') 
        index = IpProxyModel().search(ip_list[0])
        self.assertEqual(index, -1, '查询存在的数据失败') 

        # 清理测试数据
        clean_redis_test_data(ip_list)
    
    def test_resolving_html(self):
        with open(os.path.join(settings.BASE_DIR, 'kuaidaili.html'), 'r') as f:
            html = f.read()
        data = resolving_html(html)
        self.assertIsInstance(data, list, '类型异常')
        self.assertEqual(len(data), 15, '解析异常')

    def test_check_url(self):
        data = {
            "ip": "103.224.82.232",
            "port": "3128",
            "niming": "高匿名",
            "method": "HTTP",
            "addr": "江西省宜春市  联通",
            "speed": "0.7秒",
            "last_time": "2020-07-23 19:31:01",
        }
        self.assertEqual(check_url(data), True, '可用代理 测试 验证失败')
        data.update(ip="183.62.60.100")
        self.assertEqual(check_url(data), False, "不可用代理测试失败")

